
function CreateProgram(gl, vsSource, fsSource) {
    //创建程序对象
    const program = gl.createProgram();
    //创建着色对象
    const vShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
    const fShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
    //顶点着色器装进程序对象
    gl.attachShader(program, vShader);
    //片元着色器装进程序对象
    gl.attachShader(program, fShader);
    //webgl上下文对象和程序对象
    gl.linkProgram(program);
    return program;
}
//初始化着色器
function initShaders(gl, vsSource, fsSource) {
    //创建程序对象
    const program = gl.createProgram();
    //创建着色对象
    const vShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
    const fShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
    //顶点着色器装进程序对象
    gl.attachShader(program, vShader);
    //片元着色器装进程序对象
    gl.attachShader(program, fShader);
    //webgl上下文对象和程序对象
    gl.linkProgram(program);
    //启动程序对象
    gl.useProgram(program);
    //把程序对象挂到上下文对象上
    gl.program = program;
    return true;
}
//加载Shader(画笔,着色器类型,着色器文本)
function loadShader(gl, type, source) {
    const shader = gl.createShader(type);
    gl.shaderSource(shader, source);
    gl.compileShader(shader);
    return shader;
}

//线性比例尺
function ScaleLinear(ax, ay, bx, by) {
    const delta = {
        x: bx - ax,
        y: by - ay,
    };
    const k = delta.y / delta.x;
    const b = ay - ax * k;
    return function (x) {
        return k * x + b;
    };
}

//Image统一加载
function imgPromise(img) {
    return new Promise((resolve) => {
        img.onload = function () {
            resolve(img);
        }
    });
}

export {
    CreateProgram,
    initShaders,
    ScaleLinear,
    imgPromise,
}